import request from '@/utils/request'
import { getToken } from '@/utils/auth'
import axios from "axios";
const baseURL = process.env.VUE_APP_BASE_API
// 根据文件id查询文件路径
export function list(id) {
  return request({
    url: '/v1/GET/file/path/' + id,
    method: 'get',
  })
}

// 通用文件下载请求
export function fileDownload(id) {
  axios({
    url: baseURL + '/v1/POST/file/download/' + id,
    method: 'post',
    responseType:'blob',
    headers: { 'Authorization': 'Bearer ' + getToken() }
  }).then( (res) => {
    const blob = new Blob([res.data], {
      type: 'application/msword;charset=utf-8'
    });
    let fileName = decodeURI(res.headers["download-filename"]);
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = fileName
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, fileName)
    }
  })

}
// 通用预览请求
export function fileShow(id) {
  return request({
    url: '/v1/GET/file/show/' + id,
    method: 'post',
  })
}


